package com.mvp.view.activity.main;

import android.os.Bundle;
import android.widget.TextView;

import com.google.gson.Gson;
import com.mvp.R;
import com.mvp.bean.UserBean;
import com.mvp.presenter.user.UserPresenter;
import com.mvp.presenter.user.UserPresenterImpl;
import com.mvp.utils.Func;
import com.mvp.utils.LogUtils;
import com.mvp.view.activity.base_activity.BaseActivity;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.pedant.SweetAlert.SweetAlertDialog;
import okhttp3.ResponseBody;

public class MainActivity extends BaseActivity implements MainView {

    //@BindView(R.id.main_tv)
    TextView textView;
    private SweetAlertDialog dialog;
    private UserPresenter mPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initData();

    }
    private void initData() {
        mPresenter = new UserPresenterImpl(this);
        Map<String, Object> map = new HashMap<>();
        map.put("tel","155211394982");
        //map.put("requestContent", "{}");
        mPresenter.getInit(map);

    }

    @Override
    public void showDialog(String msg) {
        dialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE);
        dialog.setTitleText(msg);
        dialog.show();
    }

    @Override
    public void hideDialog() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    @Override
    public void showError(String msg) {
        if (null != dialog) {
            dialog.dismiss();
        }
        dialog = new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE);
        dialog.setTitleText(msg);
        dialog.show();

    }

    @Override
    public void onInitFinished(ResponseBody userBean) {
        //注意这里用第一个Response参数的
        String jsonString="";

        try{
             jsonString =  userBean.string();
        }catch (IOException e)
        {
            LogUtils.d("jsonString"+e.toString());
        }
        LogUtils.d("api","获取json ====res start==="+ Func.formatJson(jsonString, " ")+ "\n===res end===\n");



        //再使用Retrofit自带的JSON解析（或者别的什么）
        UserBean dpHardWareList = new Gson().fromJson(jsonString, UserBean.class);
            textView.setText(dpHardWareList.getError_code());


    }

    @Override
    public void onRefreshFinished(ResponseBody userBean) {

    }

    @Override
    public void onLoadMoreFinished(ResponseBody userBean) {

    }
}
